package com.edgclub.edg.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.FindCartoonfragmentAdapter;
import com.edgclub.edg.api.FindApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.PostsParam;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.RecommendTopic;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.FindCartoonData;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.otto.ThreadEnforcer;

import java.util.ArrayList;

/**
 * 漫画
 */
public class FindCartoonFragment extends GzBaseFragment {

    private Activity mActivity;


    private UserPrefUtils userPrefUtils;
    private FindApi findApi;
    private GlideUtils glideUtils;
    private boolean isFirstLoad = true;
    private int currPage = 1;
    private int pageSize = 20;

    private XRecyclerView recyclerView;
    private FindCartoonfragmentAdapter adapter;

    private ArrayList<RecommendTopic> cartoons;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = this.getActivity();
        return inflater.inflate(R.layout.fragment_find_video, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).register(this);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser && isFirstLoad){
            isFirstLoad = false;
            getVideoList();
        }else{

        }
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    private void initView(View view) {
       initLoadView(view);

        recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
//        recyclerView.setLoadingMoreEnabled(false);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currPage = 1;
                getVideoList();
            }

            @Override
            public void onLoadMore() {
                getVideoList();
            }
        });


    }

    private void initData() {
        findApi = new FindApi(getActivity());
        userPrefUtils = new UserPrefUtils(getActivity());
        glideUtils = new GlideUtils();
        cartoons = new ArrayList<>();
        adapter = new FindCartoonfragmentAdapter(mActivity);
        recyclerView.setAdapter(adapter);

    }

    private boolean edgLoading = false;
    private boolean isEdgEnd = false;

    private void getVideoList() {
        if (edgLoading || isEdgEnd) {
            return;
        }
        User user = userPrefUtils.getUser();
        PostsParam nearbyParam = new PostsParam();
        nearbyParam.setUserId(user.getId() + "");
        nearbyParam.setPage(currPage+"");
        nearbyParam.setPageSize(pageSize+"");
        findApi.cartoon(nearbyParam, new getIndexListener());//关注列表
    }


    class getIndexListener implements VolleyListener {

        @Override
        public void onStart(String tag) {
            edgLoading = true;

        }

        @Override
        public void onSuccess(String response, String tag) {
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
            edgLoading = false;
            setLoadViewGONE();
            if (JSONHelper.isSuccess(response)) {
                FindCartoonData data = JsonUtil.getMode(response, FindCartoonData.class);
                if (null != data && null != data.getData().getCartoons() && data.getData().getCartoons().size()>0) {
                    if(currPage == 1){
                        cartoons.clear();
                    }
                    cartoons.addAll(data.getData().getCartoons());
                    adapter.setDatas(cartoons);
                    currPage++;
                }else{
                    recyclerView.noMoreLoading();
                }
                if(data!=null && data.getData().getStars()!=null){
                    adapter.setStars(data.getData().getStars());
                }
                adapter.notifyDataSetChanged();
            } else {

            }

        }

        @Override
        public void onFaile(VolleyError volleyError, String tag) {
            dismissDialog();
            edgLoading = false;
            setNoNetWork(new ReLoadClickListener() {
                @Override
                public void reload() {
                    getVideoList();
                }
            });
            ResultCode.toastVolleyError(getActivity(), volleyError);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).unregister(this);
    }
}
